import * as echarts from 'echarts'
import "echarts/theme/dark";

export function useEchart() {

    let echartInstance = null

    const initEchart = (el) => {
        if (echartInstance) {
            echartInstance.dispose()
        }
        echartInstance = echarts.init(el)
        return echartInstance
    }

    const setOptions = (options) => {
        echartInstance?.setOption(options)
    }

    const updateSize = () => {
        echartInstance?.resize()
    }

    window.addEventListener('resize', () => {
        updateSize()
    })

    return {
        initEchart,
        setOptions,
        updateSize
    }
}